import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int temp = 0;
        int array[] = {3,6,2,7,8,5};
        //flag为标记符
        boolean flag = false;

        //冒泡法排序，时间复杂度T(n^2)
        for(int i = 0; i < array.length -1; i++){
            for(int j = 0; j < array.length - 1 - i; j++){
                if(array[j] > array[j+1]) {
                    flag = true;
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
            System.out.println("第"+(i+1)+"趟排序后的数组");
            System.out.println(Arrays.toString(array));

            if(!flag){
                break;
            }else{
                flag = false;
            }
        }
    }
}
